package ll.sms.dao;

import java.util.ArrayList;
import java.util.List;

import ll.sms.db.SqlBuilder;
import ll.sms.exceptions.SmsDBException;
import ll.sms.pojo.SmsMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class SmsMessageDAO extends BaseDAO {
	private static Log log = LogFactory.getLog(SmsMessageDAO.class);

	public void saveMessage(SmsMessage message) {
		// mid, mfrom, mto, mcontent,msendtime, maddtime,msendtype,
		//mstatue,mttype, msmsflag, mchannel

		try {
			String sql = SqlBuilder.getSql("SAVE_MESSAGE",
					message.getMid(),
					message.getMfrom(),
					//message.getMto(),
					message.getMcontent(),
					message.getMsendtime(),
					message.getMaddtime(),
					message.getMsendtype(),// DATE
					message.getMstatue(),
					message.getMttype(),
					message.getMsmsflag(),
					message.getMchannel(),
					message.getMproxyid()
					);
			this.update(sql, getConnection());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			throw new SmsDBException(e);

		}

	}

/*	public int updateMessage(SmsMessage message){
		 try {
			 String sql = SqlBuilder.getSql("UPFATE_MESSAGE",

						message.getMfrom(),
						message.getMto(),
						message.getMcontent(),
						message.getMsendtime(),
						message.getMaddtime(),
						message.getMsendtype(),// DATE
						message.getMstatue(),
						message.getMttype(),
						message.getMsmsflag(),
						message.getMchannel(),
						message.getMproxyid(),
						message.getMid()
						);

				return this.update(sql, getConnection());
		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }

	}*/

	public List<SmsMessage> findMessageByProperties(String fieldName, Object value) {
		try {
			String sql = SqlBuilder.getSql("QUERY_MESSAGE_BY_PROPERTY", SqlBuilder.toPropertyString(fieldName),
					value);

			ArrayList<SmsMessage> list = this.searchToBeanList(getConnection(),
					SmsMessage.class, sql);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			throw new SmsDBException(e);

		}
	}


	public List<SmsMessage> findAllMessages(){
		 try {
			 String sql = SqlBuilder.getSql("QUERY_ALL_MESSAGE");
			 return this.searchToBeanList(getConnection(), SmsMessage.class, sql);
		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }
	}

}
